package com.itbaizhan.controller;

import com.alibaba.cloud.ai.dashscope.audio.DashScopeAudioTranscriptionOptions;
import com.alibaba.cloud.ai.dashscope.audio.transcription.AudioTranscriptionModel;
import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;

/**
 * 语音识别
 */
@RestController
public class SttController {

    @Autowired
    private AudioTranscriptionModel audioTranscriptionModel;

    private final  String audioPath = "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_female2.wav";

    /**
     * 语音识别
     * @return
     */
    @GetMapping("/stt")
    public String stt() throws MalformedURLException {

        // 创建音频转录模型 paraformer-v2
        DashScopeAudioTranscriptionOptions transcriptionOptions = DashScopeAudioTranscriptionOptions.builder()
                .withModel("paraformer-v2")
                .build();
        // 创建音频转录的提示  oss对象存储服务器
        AudioTranscriptionPrompt audioTranscriptionPrompt = new AudioTranscriptionPrompt(new UrlResource(audioPath), transcriptionOptions);
        // 调用模型
        AudioTranscriptionResponse response = audioTranscriptionModel.call(audioTranscriptionPrompt);
        return response.getResult().getOutput();


    }


}
